package com.jie.jie.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.icu.util.Calendar;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.View;


/**
 * Created by Administrator on 2017/4/20.
 */

public class MyClockView extends View {

    private Paint hPoint;
    private Paint mPoint;
    private Paint sPoint;
    private Paint roundPaint;
    private Paint mArcPaint;
    private int blen = 50;
    private int slen = 30;
    private int fixAngle = 6;
    private int radius = 300;
    private Rect rect;
    private int h,m, s;

    public MyClockView(Context context) {
        super(context);
        init();
    }

    public MyClockView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    public void init() {
        hPoint = new Paint();
        hPoint.setColor(Color.BLACK);
        hPoint.setStrokeWidth(10);
        mPoint = new Paint();
        mPoint.setColor(Color.BLUE);
        mPoint.setStrokeWidth(8);
        sPoint = new Paint();
        sPoint.setColor(Color.RED);
        sPoint.setStrokeWidth(5);
        mArcPaint = new Paint();
        mArcPaint.setAntiAlias(true);
        mArcPaint.setStrokeWidth(5);
        roundPaint = new Paint();
        roundPaint.setStyle(Paint.Style.FILL);
        roundPaint.setAntiAlias(true);
        roundPaint.setColor(Color.BLUE);
        rect = new Rect();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawArc(canvas);
        drawRound(canvas);
        drawHMS(canvas);
        postInvalidateDelayed(1000);
    }

    private void drawHMS(Canvas canvas) {
        h = Calendar.getInstance().get(Calendar.HOUR);
        m = Calendar.getInstance().get(Calendar.MINUTE);
        s = Calendar.getInstance().get(Calendar.SECOND);
        canvas.save();
        drawH(canvas);
        drawM(canvas);
        drawS(canvas);
        canvas.restore();
    }

    private void drawH(Canvas canvas) {
        int i = h * fixAngle*5-90;
        canvas.rotate(i);
        canvas.drawLine(0,0,(float) ((radius*2/5)*Math.sin(i/180*2*Math.PI)),(float) ((radius*2/5)*Math.cos(i/180*2*Math.PI)),hPoint);
    }

    private void drawM(Canvas canvas) {
        int i = m * fixAngle-90;
        canvas.rotate(i);
        canvas.drawLine(0,0,(float) ((radius*3/5)*Math.sin(i/180*2*Math.PI)),(float) ((radius*3/5)*Math.cos(i/180*2*Math.PI)),mPoint);

    }

    private void drawS(Canvas canvas) {
        int i = s * fixAngle-90;
        canvas.rotate(i);
        canvas.drawLine(0,0,(float) ((radius*4/5)*Math.sin(i/180*2*Math.PI)),(float) ((radius*4/5)*Math.cos(i/180*2*Math.PI)),sPoint);

    }

    private void drawRound(Canvas canvas) {
        canvas.drawCircle(0,0,10,roundPaint);
    }

    private void drawArc(Canvas canvas) {
        canvas.translate(getMeasuredWidth()/2,getMeasuredHeight()/2);
        int currentP = 0;
        String text;
        int len = 0;
        mArcPaint.setStyle(Paint.Style.FILL);
        mArcPaint.setColor(Color.BLACK);

        for (int i= 0;i<360/fixAngle;i++ ) {
            if (i % 5 == 0) {

                if (i == 0) {
                    text = "12";
                } else {
                    text = currentP + "";
                }
                currentP++;
                mArcPaint.setTextSize(40);
                mArcPaint.getTextBounds(text, 0, text.length(), rect);
                canvas.drawText(text, 0, text.length(), -(rect.width() / 2), -(radius + blen + 20), mArcPaint);
                mArcPaint.setColor(Color.RED);
                len = blen;

            } else {
                mArcPaint.setColor(Color.GRAY);
                len = slen;
            }
            canvas.drawLine(0, -radius, 0, -(radius + len), mArcPaint);
            canvas.rotate(fixAngle);
        }
    }
}
